<?php
include '../../auth/ceklogin.php';
include '../../sql/conn.php';

$page = isset($_REQUEST['page']) ? intval($_REQUEST['page']) : 1;
$rows = isset($_REQUEST['rows']) ? intval($_REQUEST['rows']) : 10;
$offset = ($page-1)*$rows;
$result = array();

$inquiry = " inquiry.no_inquiry like '%%' ";

if (isset($_REQUEST['ref']))
{
    $ref = $_REQUEST['ref'];
    $ref = explode('&',$ref);

    //inquiry
    $inquiry    = $ref[0];
    $inquiry    = explode('=',$inquiry);
    $inquiry    = $inquiry[1];

    //customer
    $customer	= $ref[1];	
    $customer = explode('=',$customer);
    $customer = $customer[1];

    //tanggal 1
    $tanggal1	= $ref[2];
    $tanggal1 = explode('=',$tanggal1);
    $tanggal1 = $tanggal1[1];
    if($tanggal1!=''){
        $tanggal1 = explode("-",$tanggal1);
        $tanggal1 = $tanggal1[0]."-".$tanggal1[1]."-".($tanggal1[2]);
    }
    

    //tanggal 2
    $tanggal2 = $ref[3];
    $tanggal2 = explode('=',$tanggal2);
    $tanggal2 = $tanggal2[1];
    if($tanggal2!=''){
        $tanggal2 = explode("-",$tanggal2);
        $tanggal2 = $tanggal2[0]."-".$tanggal2[1]."-".($tanggal2[2]+1);
    }
    //cek REQUEST inquiry
    if ($inquiry!='')
    {
        $inquiry = " inquiry.no_inquiry like '%".trim($inquiry)."%' ";
    }else{
        $inquiry = " inquiry.no_inquiry like '%%' ";
    }

    //cek REQUEST customer
    if ($customer!='')
    {
            $customer	= " AND inquiry.idCustomer like '%".trim($customer)."%'";
    }else{
        $customer = '';
    }

    //cek REQUEST tanggal
    if ($tanggal2!='')
    {
            $tanggal	= " AND inquiry.crtdate between '".trim($tanggal1)."' and '".trim($tanggal2)."'";
    }else{
        $tanggal = "";
    }
}
if($_REQUEST['task']=="QgetI"){
    $status = " AND inquiry.status='OSIN'";
}

$rs = mysql_query("select count(*) from inquiry where $inquiry $customer $tanggal $status");  
$row = mysql_fetch_row($rs);  
$result["total"] = $row[0];  
  
$rs = mysql_query("
    select 
	inquiry.id
	,inquiry.no_inquiry
	,inquiry.text
	,inquiry.attn
	,inquiry.telp
	,inquiry.fax
	,inquiry.desc
	,inquiry.jns_inquiry
        ,inquiry.status
	,inquiry.crtby
	,date_format(inquiry.crtdate, '%e %b %Y') as crtdate
	,inquiry.chgby
	,date_format(inquiry.chgdate, '%e %b %Y') as chgdate
	,inquiry.idCustomer
	,customer.namaCustomer 
    from 
	inquiry
	left join customer on inquiry.idCustomer=customer.idCustomer
    where
        $inquiry
        $customer
        $tanggal
        $status
    limit $offset,$rows
        ");  

    $items = array();  
    while($row = mysql_fetch_object($rs)){  
        array_push($items, $row);  
    }  
    $result["rows"] = $items;  

echo json_encode($result);  
?>
